package com.zxbit.flash.common;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.zxbit.flash.PopApplication;
import com.zxbit.flash.cache.Acache;
import com.zxbit.flash.cache.AcacheKeys;
import com.zxbit.flash.bean.AuthData;

public class UserInfo {

    public static String getToken() {
        return Acache.get(PopApplication.getContext()).getAsString(AcacheKeys.TOKEN);
    }

    public static void setToken(String token) {
        Acache.get(PopApplication.getContext()).put(AcacheKeys.TOKEN, token);
    }

    public static void logout() {
        Acache.get(PopApplication.getContext()).remove(AcacheKeys.TOKEN);
        Acache.get(PopApplication.getContext()).remove(AcacheKeys.AUTH);
        Acache.get(PopApplication.getContext()).remove(AcacheKeys.BACK);
    }

    public static boolean isLogin() {
        if (TextUtils.isEmpty(getToken())) {
            return false;
        }

        return true;
    }


    public static String getSaveUuid() {
        String auth_data = Acache.get(PopApplication.getContext()).getAsString(AcacheKeys.AUTH);
        String backUuid = Acache.get(PopApplication.getContext()).getAsString(AcacheKeys.BACK);
        if(!TextUtils.isEmpty(backUuid)){
            return backUuid;
        } else if (!TextUtils.isEmpty(auth_data)) {
            Gson gson = new Gson();
            AuthData authData = gson.fromJson(auth_data, AuthData.class);
            return authData.getUserUuid();
        }

        return "";
    }


    public static String getShowUuid() {
        String backUuid = Acache.get(PopApplication.getContext()).getAsString(AcacheKeys.BACK);
        String auth_data = Acache.get(PopApplication.getContext()).getAsString(AcacheKeys.AUTH);
        if(!TextUtils.isEmpty(backUuid)){
            return backUuid;
        }
        else if (!TextUtils.isEmpty(auth_data)) {
            Gson gson = new Gson();
            AuthData authData = gson.fromJson(auth_data, AuthData.class);
            if (isShowUserInformation()) {
                return authData.getPreviousUserUuid();
            }
            return authData.getUserUuid();
        }

        return "";
    }


    public static boolean isShowUserInformation() {
        String is_back = Acache.get(PopApplication.getContext()).getAsString(AcacheKeys.BACK);
        String auth_data = Acache.get(PopApplication.getContext()).getAsString(AcacheKeys.AUTH);
        if (!TextUtils.isEmpty(is_back)) return true;
        else if (!TextUtils.isEmpty(auth_data)) {
            Gson gson = new Gson();
            AuthData authData = gson.fromJson(auth_data, AuthData.class);
            return authData.getIsEcho() == 2;
        }

        return false;
    }

    public static boolean isUserInformationWrong() {
        String isBack = Acache.get(PopApplication.getContext()).getAsString(AcacheKeys.BACK);
        if (TextUtils.isEmpty(isBack))
            return false;
        return true;
    }

}
